package com.gray.blog.controller.sys;

import cn.hutool.core.util.StrUtil;
import com.gray.blog.arg.sys.AddSysUserArg;
import com.gray.blog.arg.sys.GetPageUserArg;
import com.gray.blog.arg.sys.UpdateSysUserInfoArg;
import com.gray.blog.common.annotation.IpCheckTrigger;
import com.gray.blog.common.controller.BaseController;
import com.gray.blog.common.enums.ErrorCodeEnum;
import com.gray.blog.common.result.ModelResult;
import com.gray.blog.common.util.BeanUtil;
import com.gray.blog.result.sys.GetPageUserResult;
import com.gray.blog.result.sys.GetSysUserByIdResult;
import com.gray.blog.result.sys.GetUserInfoByNameResult;
import com.gray.blog.result.sys.UpdateUserInfoResult;
import com.gray.blog.service.sys.SysUserService;
import com.gray.blog.vo.sys.AddSysUserVO;
import com.gray.blog.vo.sys.GetPageUserVO;
import com.gray.blog.vo.sys.GetUserInfoByNameVO;
import com.gray.blog.vo.sys.UpdateSysUserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "/sys/user")
@Api(value = "用户信息接口", description = "用户信息相关接口")
@CrossOrigin
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping(value = "/", method = RequestMethod.POST)
    @IpCheckTrigger
    @ApiOperation(value = "分页获取用户信息")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult<GetPageUserResult> getPageUser(@RequestBody @Valid GetPageUserArg arg) {
        if (arg.isWrongParam()) {
            log.error("SysUserController.getPageUser param error arg:{}", arg);
            return ModelResult.newError(ErrorCodeEnum.PARAMS_ERROR);
        }
        GetPageUserVO vo = BeanUtil.copy(arg, GetPageUserVO.class);
        return sysUserService.getPageUser(vo);
    }

    @RequestMapping(value = "/{id}/", method = RequestMethod.GET)
    @IpCheckTrigger
    @ApiOperation(value = "根据id换取用户信息")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult<GetSysUserByIdResult> getSysUserById(@PathVariable("id") String id) {
        if (StrUtil.isBlank(id)) {
            log.error("SysUserController.getSysUserById id is null");
            return ModelResult.newError(ErrorCodeEnum.PARAMS_ERROR);
        }
        return sysUserService.getSysUserById(id);
    }

    @RequestMapping(value = "/add/", method = RequestMethod.POST)
    @IpCheckTrigger
    @ApiOperation(value = "新增用户")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult addSysUser(@RequestBody @Valid AddSysUserArg arg) {
        AddSysUserVO vo = BeanUtil.copy(arg, AddSysUserVO.class);
        return sysUserService.addUser(vo);
    }

    @RequestMapping(value = "/{id}/", method = RequestMethod.DELETE)
    @IpCheckTrigger
    @ApiOperation(value = "根据id删除用户信息")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult deleteUser(@PathVariable("id") String id) {
        if (StringUtils.isBlank(id)) {
            log.error("SysUserController.deleteUser id is null");
            return new ModelResult(ErrorCodeEnum.PARAMS_ERROR);
        }
        return sysUserService.deleteUser(id);
    }

    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @IpCheckTrigger
    @ApiOperation(value = "根据id修改用户信息")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult<UpdateUserInfoResult> updateUser(@RequestBody @Valid UpdateSysUserInfoArg arg) {
        UpdateSysUserInfoVO vo = BeanUtil.copy(arg, UpdateSysUserInfoVO.class);
        if (arg.isWrongParam()) {
            log.error("SysUserController.updateUser param is error", arg);
            return new ModelResult<>(ErrorCodeEnum.PARAMS_ERROR);
        }
        return sysUserService.updateUserInfo(vo);
    }

    /**
     * 以下为接口为普通成员提供
     **/
    @RequestMapping(value = "/name/{name}/", method = RequestMethod.GET)
    @IpCheckTrigger
    @ApiModelProperty(value = "根据名称获取用户普通信息")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN, ORDINARY_USER})
    public ModelResult<GetUserInfoByNameResult> getUserByName(@PathVariable("name") String name) {
        if (StrUtil.isBlank(name)) {
            log.warn("SysUserController.getUserInfoByName error name is null name:{}", name);
            return new ModelResult<>(ErrorCodeEnum.PARAMS_ERROR);
        }
        GetUserInfoByNameVO vo = new GetUserInfoByNameVO();
        vo.setUserName(name);
        return sysUserService.getUserInfoByName(vo);
    }

}
